package com.zmn.mcc.services.impl.staff.securitylog;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.mcc.business.interfaces.staff.ip.StaffIpInfoBService;
import com.zmn.mcc.model.entity.staff.log.IpInfoVo;
import com.zmn.mcc.model.entity.staff.securitylog.StaffSecurityLog;
import com.zmn.mcc.model.entity.staff.securitylog.StaffSecurityLogQuery;
import com.zmn.mcc.model.vo.StaffSecurityLogVo;
import com.zmn.mcc.persistence.interfaces.staff.securitylog.StaffSecurityLogDAO;
import com.zmn.mcc.services.interfaces.staff.securitylog.StaffSecurityLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 描述: 员工安全日志表基础接口实现类
 *
 * @author tanghongjun
 * @since 2020-06-20 14:17:03
 **/
@Slf4j
@Service
public class StaffSecurityLogServiceImpl implements StaffSecurityLogService {

    private static final String TAG = "员工安全日志表基础服务接口";

    @Resource
    private StaffSecurityLogDAO staffSecurityLogDao;

    @Resource
    StaffIpInfoBService staffIpInfoBService;
    /**
     * 描述: 根据id查询数据
     *
     * @param logId id
     * @author tanghongjun
     * @since 2020-06-20 14:17:03
     **/
    @Override
    public StaffSecurityLog findById(Integer logId) {

        if (Objects.isNull(logId)) {
            log.error("[{}],根据id查询数据logId为null", TAG);
            return null;
        }
        return staffSecurityLogDao.findById(logId);
    }

    /**
     * 描述: 按条件查询数据 支持分页
     *
     * @param query 查询对象
     * @author tanghongjun
     * @since 2020-06-20 14:17:03
     **/
    @Override
    public List<StaffSecurityLogVo> listPageByQuery(StaffSecurityLogQuery query) {

        int count = staffSecurityLogDao.countByQuery(query);
        query.setTotalCount(count);

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        List<StaffSecurityLog> staffSecurityLogList = staffSecurityLogDao.listPageByQuery(query);
        if (CollectionUtil.isNullOrEmpty(staffSecurityLogList)) {
            return new ArrayList<>();
        }

        List<StaffSecurityLogVo> staffSecurityLogVoList = staffSecurityLogList.stream().map(e ->{
            StaffSecurityLogVo vo = new StaffSecurityLogVo();
            BeanUtils.copyProperties(e, vo);
            return vo;
        }).collect(Collectors.toList());
        //查询ip所在地
        List<String> ipList = staffSecurityLogList.stream().map(StaffSecurityLog::getOpertorIp).collect(Collectors.toList());
        List<IpInfoVo> ipInfoList = staffIpInfoBService.listIpInfo(ipList);
        Map<String, IpInfoVo> ipMap = ipInfoList.stream()
                .collect(Collectors.toMap(IpInfoVo::getIp, e -> e, (e1, e2) -> e2));

        //设置所在地
        for (StaffSecurityLogVo staffSecurityLogVo : staffSecurityLogVoList) {
            String ip = staffSecurityLogVo.getOpertorIp();
            String location = ipMap.get(ip) == null ? "" : ipMap.get(ip).getProvince() + "-" + ipMap.get(ip).getCity();
            staffSecurityLogVo.setLocation(location);
        }

        return staffSecurityLogVoList;
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query 查询对象
     * @author tanghongjun
     * @since 2020-06-20 14:17:03
     **/
    @Override
    public List<StaffSecurityLog> listByQuery(StaffSecurityLogQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return staffSecurityLogDao.listByQuery(query);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query 查询对象
     * @author tanghongjun
     * @since 2020-06-20 14:17:03
     **/
    @Override
    public Integer countByQuery(StaffSecurityLogQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return staffSecurityLogDao.countByQuery(query);
    }

    /**
     * 描述: 根据id修改数据 id为必传
     *
     * @param staffSecurityLog 待修改数据
     * @return java.lang.Integer
     * @author tanghongjun
     * @since 2020-06-20 14:17:03
     **/
    @Override
    public Integer updateByKey(StaffSecurityLog staffSecurityLog) {

        if (Objects.isNull(staffSecurityLog.getLogId())) {
            log.error("[{}],根据id修改数据,staffSecurityLog为null", TAG);
            return null;
        }
        return staffSecurityLogDao.updateByKey(staffSecurityLog);
    }

    /**
     * 描述: 新增数据
     *
     * @param staffSecurityLog 待新增数据
     * @return java.lang.Integer 主键key
     * @author tanghongjun
     * @since 2020-06-20 14:17:03
     **/
    @Override
    public Integer insert(StaffSecurityLog staffSecurityLog) {

        if (Objects.isNull(staffSecurityLog)) {
            log.error("[{}],新增数据,staffSecurityLog为null", TAG);
            return null;
        }
        return staffSecurityLogDao.insert(staffSecurityLog);
    }

    /**
     * 描述: 根据主键删除数据
     *
     * @param logId
     * @return: 执行结果码
     * @auther: tanghongjun
     * @since: 2020-06-20 14:17:03
     */
    @Override
    public Integer deleteByKey(Integer logId) {

        if (Objects.isNull(logId)) {
            log.error("[{}],根据id删除数据,logId为null", TAG);
            return null;
        }
        return staffSecurityLogDao.deleteByKey(logId);
    }

}